﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class NewRecipe : System.Web.UI.Page
{
    PIMServices pimService = new PIMServices();
    protected void Page_Load(object sender, EventArgs e)
    {
        ScriptManager.RegisterStartupScript(Page, typeof(Page), "Call InitRating", "initRating()", true);
    }
    protected void btnSave_Click(object sender, EventArgs e)
    {
        try
        {
            string userId = "";

            if (Session["Userid"] != null)
                userId = Session["Userid"].ToString();
            string[] _Ingredient = new string[0];
            string _strIngredient = txtIngredient.Text.Replace("\r\n", "|");
            if (!string.IsNullOrEmpty(txtIngredient.Text))
                _Ingredient = _strIngredient.Split('|');
            string a = txtIngredient.Text;
            double rating = 5;
            if (!string.IsNullOrEmpty(ratingStar.Value))
                rating = Convert.ToDouble(ratingStar.Value);
            int result = pimService.InsertRecipe(Convert.ToInt32(userId), Convert.ToInt32(userId), 1, txtRecipeName.Text, Convert.ToInt32(txtServing.Text), 6, 7, hdImage.Value, txtDescription.Text,
                                            rating, txtRecDirection.Text, txtIngredient.Text, chIsPublish.Checked);
            if (result > -1)
            {
                if (_Ingredient.Count() > 0)
                {
                    pimService.DeleteAllRecipeIngredient(result);
                    foreach (string str in _Ingredient)
                    {
                        string _rs = pimService.InsertRecipeIngredientUser(result, str, 0, 0, 0);
                    }
                }
                // ScriptManager.RegisterStartupScript(this, this.Page.GetType(), "Success", "alert('Insert Recipe Successful')", true);
                Response.Redirect("MyRecipe.aspx");
            }
            else
            {
                ScriptManager.RegisterStartupScript(this, this.Page.GetType(), "Error", "alert('Can not Create Recipe')", true);
            }
        }
        catch (Exception ex)
        {

        }
    }
}